package com.linjiahao.userCenter.util;

import com.alibaba.fastjson.JSON;
import com.linjiahao.userCenter.domain.ChatUserInfo;
import com.linjiahao.userCenter.vo.PersonInfoVO;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.text.Collator;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
 * @ClassName PinYinUtil
 * @Description: TODO
 * @Author linjiahao
 * @Date 7/1/2022
 **/
public class PinYinUtil {

    /**
    * 获取姓名全拼和首字母
	 * @param  chinese 汉语名称
	 * @return fullPinyin : 全拼        simplePinyin ： 首字母  groupPinyin：微信用户组第一个字母
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
    public static Map<String, String> changeChinesePinyin(String chinese) {
        Map<String, String> pinyin = new HashMap<String, String>();

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuffer fullPinyin = new StringBuffer();
        StringBuffer simplePinyin = new StringBuffer();
        StringBuffer firstPinyin = new StringBuffer();

        char[] chineseChar = chinese.toCharArray();
        for (int i = 0; i < chineseChar.length; i++) {
            String[] str = null;
            try {
                str = PinyinHelper.toHanyuPinyinStringArray(chineseChar[i],
                        format);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            if (str != null) {
                fullPinyin = fullPinyin.append(str[0].toString());
                simplePinyin = simplePinyin.append(str[0].charAt(0));

            }
            if (str == null) {
                String regex = "^[0-9]*[a-zA-Z]*+$";
                Pattern pattern = Pattern.compile(regex);
                Matcher m = pattern.matcher(String.valueOf(chineseChar[i]));
                if (m.find()) {
                    fullPinyin = fullPinyin.append(chineseChar[i]);
                    simplePinyin = simplePinyin.append(chineseChar[i]);
                }
            }
        }
        String[] name = new String[0];
        try {
            name = PinyinHelper.toHanyuPinyinStringArray(chineseChar[0],format);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        firstPinyin=firstPinyin.append(name[0].charAt(0));
        pinyin.put("fullPinyin", fullPinyin.toString());
        pinyin.put("simplePinyin", simplePinyin.toString().toUpperCase());
        pinyin.put("groupPinyin", firstPinyin.toString().toUpperCase());
        return pinyin;
    }
    /**
     * 按拼音首字母分组
     * @param list
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static Map<String, Object> getCodeGroup(List<String> list) throws BadHanyuPinyinOutputFormatCombination {
        Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
        //按字母排序
        Collections.sort(list, com);
        //输出26个字母
        Map<String, Object> map = new TreeMap<String, Object>();
        for(int i=1;i<=26;i++){
            String word = String. valueOf((char) (96 + i)). toUpperCase();
            //循环找出首字母一样的数据
            List<String> letter = new ArrayList<String>();
            for (String str : list) {
                String code=changeChinesePinyin(str).get("groupPinyin");
                if(word.equals(code)) {
                    letter.add(str);
                }
            }
            map.put(word, letter);
        }
        return map;
    }

    /**
     * 判断是否中文
     * @param con
     * @return
     */
    public static boolean isChinese(String con) {
        for (int i = 0; i < con.length(); i = i + 1) {
            if (!Pattern.compile("[\u4e00-\u9fa5]").matcher(
                    String.valueOf(con.charAt(i))).find()) {
                return false;
            }
        }
        return true;
    }
    /**
     * 判断是否英文
     * @param con
     * @return
     */
    private static boolean isEnglish(String con) {
        if (null != con && !"".equals(con)) {
            if ((isChinese(con) || con.matches("^[A-Za-z]+$"))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 按用户拼音首字母分组
     * @param list
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static List<PersonInfoVO> getUserCodeGroup(List<PersonInfoVO> list) throws BadHanyuPinyinOutputFormatCombination {
        //Collections工具类的sort()方法对list集合元素排序　
        Collections.sort(list,new Comparator<PersonInfoVO>(){
            @Override
            public int compare(PersonInfoVO o1, PersonInfoVO o2) {
                //获取中文环境
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getNickName(),o2.getNickName());
            }
        });
        //输出26个字母
        Map<String, List<PersonInfoVO>> map = new HashMap<>();
        for(int i=1;i<=26;i++){
            String word = String. valueOf((char) (96 + i)). toUpperCase();
            map.put(word, null);
        }
        //循环找出首字母一样的数据
        for (PersonInfoVO str : list) {
            //当前为中文或者中文
            String c = String.valueOf(str.getNickName().charAt(0));
            if(isEnglish(c)){
                String code = "";
                if(isChinese(c)){
                    code = changeChinesePinyin(str.getNickName()).get("groupPinyin");
                }else {
                    code = c.toUpperCase();
                }
                if(map.containsKey(code)){
                    str.setInitial(code);
                }else {
                    str.setInitial("#");
                }
            }else {
                str.setInitial("#");
            }
        }
        return list;
    }


    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
        ChatUserInfo userInfo = new ChatUserInfo();
        userInfo.setNickName("ljinjs");
        ChatUserInfo userInfo1 = new ChatUserInfo();
        userInfo1.setNickName("182000891281");
        ChatUserInfo userInfo2 = new ChatUserInfo();
        userInfo2.setNickName("#$%&88811");
        ChatUserInfo userInfo3 = new ChatUserInfo();
        userInfo3.setNickName("是飒飒");
        ChatUserInfo userInfo4 = new ChatUserInfo();
        userInfo4.setNickName("🙄😣😣");
        ChatUserInfo userInfo5 = new ChatUserInfo();
        userInfo5.setNickName("经济数据");
        List<ChatUserInfo> list = new ArrayList<>();
        list.add(userInfo);
        list.add(userInfo1);
        list.add(userInfo2);
        list.add(userInfo3);
        list.add(userInfo4);
        list.add(userInfo5);
        //System.out.println(JSON.toJSONString(getUserCodeGroup(list)));
    }



}
